package com.nine.mymall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.nine.mymall.product.entity.ProductAttrValueEntity;
import com.nine.mymall.product.service.ProductAttrValueService;
import com.nine.mymall.product.vo.AttrRespVo;
import com.nine.mymall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nine.mymall.product.entity.AttrEntity;
import com.nine.mymall.product.service.AttrService;
import com.nine.common.utils.PageUtils;
import com.nine.common.utils.R;


/**
 * 商品属性
 *
 * @author nine
 * @email 2685441173@qq.com
 * @date 2021-08-27 17:57:46
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 22、获取spu规格
     * /product/attr/base/listforspu/{spuId}
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId) {

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);

        return R.ok().put("data", entities);
    }


    /**
     * 05、获取分类规格参数
     *
     * @param params
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        //AttrEntity attr = attrService.getById(attrId);

        AttrRespVo respVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     *
     * 23、修改商品规格
     * /product/attr/update/{spuId}
     * [{
     * 	"attrId": 7,
     * 	"attrName": "入网型号",
     * 	"attrValue": "LIO-AL00",
     * 	"quickShow": 1
     * }, {
     * 	"attrId": 14,
     * 	"attrName": "机身材质工艺",
     * 	"attrValue": "玻璃",
     * 	"quickShow": 0
     * }, {
     * 	"attrId": 16,
     * 	"attrName": "CPU型号",
     * 	"attrValue": "HUAWEI Kirin 980",
     * 	"quickShow": 1
     * }]
     * @param spuId
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R update(@PathVariable("spuId") Long spuId, @RequestBody  List<ProductAttrValueEntity> entities) {

        productAttrValueService.updateSpuAttr(spuId, entities);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
